package history.hot100.difficult;

// 72. 编辑距离
public class LeetCode_72 {
    public int minDistance(String word1, String word2) {
        int n = word1.length();
        int m = word2.length();
        int [][]state = new int[n+1][m+1];
        for (int i = 1; i <=n; i++) state[i][0] = i;
        for (int j = 1; j <= m ; j++) state[0][j] = j;

        for (int i = 1; i <=n; i++) {
            for (int j = 1; j <= m ; j++) {
                if (word1.charAt(i-1) == word2.charAt(j-1)) {
                    state[i][j] = state[i-1][j-1];
                } else {
                    state[i][j] = Math.min(Math.min(state[i-1][j], state[i][j-1]), state[i-1][j-1])+1;
                }
            }
        }
        return state[n][m];
    }
}
